#!/bin/bash
# variables
# function
#设置环境变量
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$DM_BIN
export PATH=$PATH:$DM_BIN

ip=SYSDBA/${SYSDBA_PWD}@${DB_HOST1}:${DB_PORT1}
props=props.dm
preheats=preheat.dm

LOGDIR=`pwd`/log/01_tpcc
LOG=$LOGDIR/$filename"_"$WAREHOUSES"w_"$TERMINALS"u_"$RUNMINS"m_"`date "+%Y%m%d%H%M%S"`.log
SCRIPTS_DIR=`pwd`/01_tpcc
# new create log directory
if [ ! -d ${LOGDIR} ]; then 
  mkdir -p ${LOGDIR}
fi

#runBenchmark
cd ${SCRIPTS_DIR}/tpcc/benchmarksql5/run/
cp props.dm preheat.dm
sed -i "/^runMins/c runMins=$RUNMINS" $props
sed -i "/^runMins/c runMins=2" $preheats
sed -i "/^warehouses/c warehouses=$WAREHOUSES" $props $preheats
sed -i "/^loadWorkers/c loadWorkers=$LOADWORKERS" $props $preheats
sed -i "/^terminals/c terminals=$TERMINALS" $props $preheats
sed -i "/^conn/c conn=jdbc:dm://${DB_HOST1}:${DB_PORT1}" $props $preheats

starttime1=`date +'%Y-%m-%d %H:%M:%S'`
echo "---BEGIN---"|tee -a ${LOG}
echo "!!!tpcc start: $starttime1"|tee -a ${LOG}

echo ""|tee -a ${LOG}
echo "!!!check data!!!"|tee -a ${LOG}
echo ""|tee -a ${LOG}
$DM_BIN/disql $ip << EOF
spool $LOG append
start ${SCRIPTS_DIR}/tpcc/bms5_checkdb.sql
exit
EOF
if [ ${CKPT_FLAG} = "1" ];then
$DM_BIN/disql $ip << EOF
spool $LOG append
start ${SCRIPTS_DIR}/tpcc/bms5_ckpt.sql
exit
EOF
fi
if [ ${ACID_FLAG} = "1" ];then
$DM_BIN/disql $ip << EOF
spool $LOG append
start ${SCRIPTS_DIR}/tpcc/bms5_acid.sql
exit
EOF
fi
$DM_BIN/disql $ip << EOF
spool $LOG append
start ${SCRIPTS_DIR}/tpcc/bms5_count.sql
exit
EOF

echo ""|tee -a ${LOG}
echo "!!!check INI info!!!"|tee -a ${LOG}
echo ""|tee -a ${LOG}
$DM_BIN/disql $ip << EOF
spool $LOG append
start ${SCRIPTS_DIR}/tpcc/bms5_checkini.sql
exit
EOF

echo ""|tee -a ${LOG}
echo "!!!check INI info!!!"|tee -a ${LOG}
echo ""|tee -a ${LOG}
$DM_BIN/disql $ip << EOF
spool $LOG append
start ${SCRIPTS_DIR}/tpcc/bms5_loaddata_s3.sql
exit
EOF

echo ""|tee -a ${LOG}
echo "!!!run the benchmark!!!"|tee -a ${LOG}|tee -a ${LOG}
echo ""|tee -a ${LOG}

#collect nmon data
echo "collect nmon data"|tee -a ${LOG}
echo "nmon -f -s 3 -c 130 -m /home/hql/nmon_dir &"|tee -a ${LOG}
echo "the nmon's dir is /home/hql/nmon_dir"|tee -a ${LOG}
nmon -f -s 3 -c 130 -m /home/hql/nmon_dir &
echo "-------------------------------------------------------------------"|tee -a ${LOG}

cd ${SCRIPTS_DIR}/tpcc/benchmarksql5/run/
if [ ${PREHEAT_FALG} = "1" ] && [ ${TEST_FLAG} = "1" ];then
(./runBenchmark.sh $preheats)&
wait
fi

if [ ${TEST_FLAG} = "1" ];then
(./runBenchmark.sh $props |tee -a ${LOG})
wait
fi
echo ""|tee -a ${LOG}
echo "!!!After test,check rlog info!!!"|tee -a ${LOG}
echo ""|tee -a ${LOG}
$DM_BIN/disql $ip << EOF
spool $LOG append
start ${SCRIPTS_DIR}/tpcc/bms5_checkrlog.sql
start ${SCRIPTS_DIR}/tpcc/bms5_count.sql
exit
EOF
if [ ${ACID_FLAG} = "1" ];then
$DM_BIN/disql $ip << EOF
spool $LOG append
start ${SCRIPTS_DIR}/tpcc/bms5_acid.sql
exit
EOF
fi

endtime1=`date +'%Y-%m-%d %H:%M:%S'`
start_seconds1=$(date --date="$starttime1" +%s);
end_seconds1=$(date --date="$endtime1" +%s);

echo ""|tee -a ${LOG}
echo "!!!test tpcc used:"$((end_seconds1-start_seconds1))"s" |tee -a $LOG
rm -rf dmsql*
rm -rf dameng_result_* benchmarksql-error.log .jTPCC_run_seq.dat
